package com.cmw.controller.web;

import com.cmw.core.base.exception.ServiceException;
import com.cmw.core.base.exception.UtilException;
import com.cmw.core.util.BeanUtil;
import com.cmw.core.util.FastJsonUtil;
import com.cmw.core.util.PageHandler;
import com.cmw.core.util.StringHandler;
import com.cmw.entity.Student;
import com.cmw.service.inter.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 学生Controller
 * @author chengmingwei
 * @date 2017-05-17 09:31
 */
@Controller
@RequestMapping({"/student"})
public class StudentController {

    @Resource(name="studentService")
    StudentService studentService;

    @RequestMapping(value = "/get")
    @ResponseBody
    public String user(){
       return "My Name is chengmingwei";
    }

    /**
     * 跳转主页面
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/home")
    public String home(HttpServletRequest request, HttpServletResponse response){
        List<Student> list = studentService.getListAll();
        PageHandler.outJson(response, true, FastJsonUtil.convertList2Str(list));
        return null;
    }

    /**
     * 跳转列表页面
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/list")
    public String list(HttpServletRequest request, HttpServletResponse response){
        List<Student> list = studentService.getListAll();
        PageHandler.outJson(response, true, FastJsonUtil.convertList2Str(list));
        return null;
    }

    /**
     * 跳转详细页面
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/info")
    public String info(HttpServletRequest request, HttpServletResponse response){
        Long id = PageHandler.getLVal(request ,"id");
        if(StringHandler.isValidObj(id)) throw new ServiceException(ServiceException.BREED_IS_NULL);
        Student obj = studentService.get(id);
        PageHandler.outJson(response, true, FastJsonUtil.convertJsonToStr(obj));
        return null;
    }

    /**
     * 保存数据
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/save")
    public String save(HttpServletRequest request, HttpServletResponse response){
        try {
            Student student = BeanUtil.copyValue(Student.class, request);
            System.out.println("save...XXXX");
            System.out.println("save...XXXX");
            System.out.println("save...XXXX");
            System.out.println("save...XXXX");
            System.out.println("save...XXXX");
            System.out.println("save...XXXX");
            System.out.println("save...XXXX");
            System.out.println("save...XXXX");
            studentService.insert(student);
        } catch (UtilException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 移除数据
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/remove")
    public String remove(HttpServletRequest request, HttpServletResponse response){
        Long id = PageHandler.getLVal(request, "id");
        studentService.delete(id);
        PageHandler.outJson(response, true,"ok");
        return null;
    }
}
